//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.qst.crop.controller;

import com.qst.crop.common.Result;
import com.qst.crop.entity.*;
import com.qst.crop.service.BankService;
import com.qst.crop.service.FinanceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(
        tags = {"融资模块接口"}
)
@RestController
@RequestMapping({"/finance"})
@CrossOrigin
public class FinanceController {
    @Autowired
    private FinanceService financeService;
    @Autowired
    private BankService bankService;

    public FinanceController() {
    }

    @ApiOperation("查询融资意向")
    @GetMapping({"/selectIntentionByName"})
    public Result selectIntentionByName() {
        UserDetails principal = (UserDetails)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String name = principal.getUsername();
        List<Intention> intentions = this.financeService.selectIntentionByName(name);
        return new Result(true, 20000, "查询成功", intentions);
    }

    @ApiOperation("添加融资意向")
    @PostMapping({"/insertIntentionByName"})
    public Result insertIntentionByName(@RequestBody Intention intention) {
        UserDetails principal = (UserDetails)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String name = principal.getUsername();
        intention.setUserName(name);
        this.financeService.insertIntentionByName(intention);
        return new Result(true, 20000, "添加成功");
    }

    @ApiOperation("修改融资意向")
    @PutMapping({"/updateIntentionByName"})
    public Result updateIntentionByName(@RequestBody Intention intention) {
        UserDetails principal = (UserDetails)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String name = principal.getUsername();
        intention.setUserName(name);
        this.financeService.updateIntentionByName(intention);
        return new Result(true, 20000, "修改成功");
    }

    @ApiOperation("删除融资意向")
    @DeleteMapping({"/deleteIntentionByName"})
    public Result deleteIntentionByName() {
        UserDetails principal = (UserDetails)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String name = principal.getUsername();
        this.financeService.deleteIntentionByName(name);
        return new Result(true, 20000, "删除成功");
    }

    @ApiOperation("查询推荐融资人")
    @GetMapping({"/selectRecommendByName"})
    public Result selectRecommendByName() {
        UserDetails principal = (UserDetails)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String name = principal.getUsername();
        List<Recommend> list = this.financeService.selectRecommend(name);
        return new Result(true, 20000, "查询成功", list);
    }

    @ApiOperation("单人贷款")
    @GetMapping({"/selectFinaceUser/{bankId}"})
    public Result selectFinaceUser(@PathVariable("bankId") String bankId) {
        UserDetails principal = (UserDetails)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String name = principal.getUsername();
        FinaceUserDetails finaceUserDetails = this.financeService.selectFinaceUser(name, bankId);
        return new Result(true, 20000, (String)null, finaceUserDetails);
    }

    @ApiOperation("查询是否已经贷款")
    @GetMapping({"/selectIfApply"})
    public Result selectIfApply() {
        UserDetails principal = (UserDetails)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String name = principal.getUsername();
        boolean b = this.financeService.selectIfApply(name);
        return new Result(true, 20000, "添加成功", b);
    }

    @ApiOperation("添加单人贷款")
    @PostMapping({"/add"})
    public Result add(@RequestBody Finance finance) {
        UserDetails principal = (UserDetails)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String name = principal.getUsername();
        finance.setOwnName(name);
        this.financeService.add(finance);
        return new Result(true, 20000, "申请成功");
    }

    @ApiOperation("添加组合贷款")
    @PostMapping({"/addFinanceMulti"})
    public Result addFinanceMulti(@RequestBody Finance finance) {
        UserDetails principal = (UserDetails)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String name = principal.getUsername();
        finance.setOwnName(name);
        this.financeService.addMulti(finance);
        return new Result(true, 20000, "申请成功");
    }

    @ApiOperation("查询融资情况")
    @GetMapping({"/selectByName"})
    public Result selectByName() {
        UserDetails principal = (UserDetails)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String name = principal.getUsername();
        Finance finance = new Finance();
        finance.setOwnName(name);
        List<Finance> finances = this.financeService.selectByFinance(finance);
        return new Result(true, 20000, "查询成功", finances);
    }

    @ApiOperation("根据id修改融资情报")
    @PutMapping({"/update"})
    public Result update(@RequestBody Finance finance) {
        this.financeService.updateByFinance(finance);
        return new Result(true, 20000, "修改成功");
    }

    @ApiOperation("根据id删除融资情报")
    @DeleteMapping({"/delete/{id}"})
    public Result delete(@PathVariable("id") Integer id) {
        this.financeService.deleteByFinanceId(id);
        return new Result(true, 20000, "删除成功");
    }

    @ApiOperation("根据id查询融资情报")
    @GetMapping({"/selectById/{id}"})
    public Result selectById(@PathVariable("id") Integer id) {
        Finance finance = this.financeService.selectByFinanceId(id);
        return new Result(true, 20000, "查询成功", finance);
    }

    @ApiOperation("查询银行情报")
    @GetMapping({"/selectbank"})
    public Result selectbank() {
        List<Bank> banks = this.bankService.selectAllBank();
        return new Result(true, 20000, "查询成功", banks);
    }
}
